#include <stdio.h>
#include <sys/select.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    fd_set rfds;
    int nfds;
    int i;
    char buf[1024];
    int len;
    FD_ZERO(&rfds); //清空集合
    FD_SET(0, &rfds); //标准输入
    nfds = 0 + 1;     //最大文件描述符加1
    while (1)
    {
        fd_set fds = rfds;
        /* 开始监听 */
        if (select(nfds, &fds, NULL, NULL, NULL) < 0)
        {
            printf("select err.\n");
            return -1;
        }
        for (i = 0; i < nfds; i++)
        {
            /* 判断是否满足条件 */
            if (FD_ISSET(i, &fds))
            {
                len = read(i, buf, 1024);
                buf[len] = '\0';
                printf("read buf: %s\n", buf);
            }
        }
    }

    return 0;
}